package app.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class EncryptTools {

//AES加密

    public static String encrypt(String text, String secKey) throws Exception {

        byte[] raw = secKey.getBytes();

        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

// "算法/模式/补码方式"

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

// 使用CBC模式，需要一个向量iv，可增加加密算法的强度

        IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());

        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        byte[] encrypted = cipher.doFinal(text.getBytes());

        return Base64.getEncoder().encodeToString(encrypted);

    }

//字符填充

    public static String zfill(String result, int n) {

        if (result.length() >= n) {

            result = result.substring(result.length() - n, result.length());

        } else {

            StringBuilder stringBuilder = new StringBuilder();

            for (int i = n; i > result.length(); i--) {

                stringBuilder.append("0");

            }

            stringBuilder.append(result);

            result = stringBuilder.toString();

        }

        return result;

    }

}
